package com.ktjy.interceptors;

import com.ktjy.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 拦截器
 */
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        if(servletPath.endsWith("/login") || servletPath.endsWith("/go")){
            return true;
        }
        HttpSession session = request.getSession();
        SysUser user = (SysUser) session.getAttribute("user");
        if(null==user){
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
            return false;
        }
        return true;
    }
}
